using System;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ctsu.Admin.BusLogic;
using ctsu.Data;


namespace ctsu.Admin.Forms
{
	public class CoreLabUtils
	{
		public CoreLabUtils()
		{

		}

        //public static bool IsBartenderAvailable(out string message)
        //{
        //    message = "";
        //    StringBuilder sb = new StringBuilder();
        //    BarTender.Application btApp = null;
        //    try
        //    {
        //        btApp = new BarTender.ApplicationClass();
        //        btApp.LicenseServer.Connect();
        //        if (!btApp.LicenseServer.IsConnected)
        //        {
        //            sb.Append("Could not start bartender server");
        //            message = sb.ToString();
        //            return false;
        //        }
        //        return true;
        //    }
        //    catch (Exception ex)
        //    {
        //        //bool b = //ExceptionPolicy.HandleException(ex, "Exception Policy");
        //        sb.Append("Could not start bartender server");
        //        sb.Append(Environment.NewLine);
        //        sb.Append("Error message: " + ex.Message);
        //        message = sb.ToString();
        //        return false;
        //    }
        //    finally
        //    {
        //        if (btApp != null)
        //        {
        //            btApp.Quit(BarTender.BtSaveOptions.btDoNotSaveChanges);
        //            btApp = null;
        //        }
        //    }
        //}

		public static int GetPatientName(string patient, out string name, out string message)
		{
			name = "";
			message = "";

			if (GetPatientNameFromNew(patient, out name, out message))
				return 2;
			else
				return -1;
		}

		public static int GetPatientName(int id, out string name, out string message)
		{
			name = "";
			message = "";

			StringBuilder sb = new StringBuilder();

			string sql = "SELECT LastName, FirstName, MRN " +
				" FROM Patient WHERE ID = " + id;

			//SqlConnection conn = LoginForm.GetSqlConnection();
			IDataReader reader = null;

			try
			{
				reader = DataAccess.GetDataReader(sql);
				if (reader.Read())
				{
					if (reader.IsDBNull(0))
						name = reader.GetString(2);
					else
						name = reader.GetString(0) + ", " +
							reader.GetString(1);
					return 1;
				}
				else
					return -1;
			}
			catch (Exception ex)
			{
				sb.Append("Could not retrieve patient!");
				sb.Append(Environment.NewLine);
				sb.Append("Error message: " + ex.Message);
				//bool b = //ExceptionPolicy.HandleException(ex, "Exception Policy");
				message = sb.ToString();
				return -1;
			}
			finally
			{
				if (reader != null)
				{
					if (!reader.IsClosed)
						reader.Close();
				}
				//if(conn != null)
				//    conn.Dispose();
			}

		}

		public static ProtocolInfo GetProtocolInfo(int id, out string message)
		{
			message = "";
			ProtocolInfo pi = new ProtocolInfo();

			StringBuilder sb = new StringBuilder();

			string sql = "SELECT p.ProtocolNumber,i.LastName+','+i.FirstName AS Name " +
				" FROM Protocol p,ProtocolStaff ps,Investigator i " +
				" WHERE p.ID = " + id +
				" AND ps.ProtocolID = p.ID " +
				" AND (ps.StaffTypeID = 1 OR  ps.StaffTypeID = 4) " +
				" AND i.ID = ps.InvestigatorID";

			//SqlConnection conn = LoginForm.GetSqlConnection();
			IDataReader reader = null;
			try
			{
				reader = DataAccess.GetDataReader(sql);
				if (reader.Read())
				{
					pi.ID = id;
					//pi.SPID = reader.GetInt32(0);
					pi.Number = reader.GetString(1);
					pi.Investigator = reader.GetString(2);
				}
				return pi;
			}
			catch (Exception ex)
			{
				sb.Append("Could not retrieve protocol information!");
				sb.Append(Environment.NewLine);
				sb.Append("Error message: " + ex.Message);
				//bool b = //ExceptionPolicy.HandleException(ex, "Exception Policy");
				message = sb.ToString();
				return pi;
			}
			finally
			{
				if (reader != null)
				{
					if (!reader.IsClosed)
						reader.Close();
				}
				//if(conn != null)
				//    conn.Dispose();
			}
		}

		public static PatientInfo GetPatientInfo(int id, out string message)
		{
			message = "";
			PatientInfo pi = new PatientInfo();

			StringBuilder sb = new StringBuilder();

			string sql = "SELECT LastName, FirstName, MRN, OtherPatientID, DOB " +
				" FROM Patient WHERE ID = " + id;

			//SqlConnection conn = LoginForm.GetSqlConnection();
			IDataReader reader = null;

			try
			{
				reader = DataAccess.GetDataReader(sql);
				if (reader.Read())
				{
					pi.ID = id;
					if (!reader.IsDBNull(0))
						pi.LastName = reader.GetString(0);
					if (!reader.IsDBNull(1))
						pi.FirstName = reader.GetString(1);
					if (!reader.IsDBNull(2))
						pi.MRN = reader.GetString(2);
					if (!reader.IsDBNull(3))
						pi.OtherID = reader.GetString(3);
					if (!reader.IsDBNull(4))
						pi.DateOfBirth = reader.GetDateTime(4);
				}
				return pi;
			}
			catch (Exception ex)
			{
				sb.Append("Could not retrieve patient information!");
				sb.Append(Environment.NewLine);
				sb.Append("Error message: " + ex.Message);
				//bool b = //ExceptionPolicy.HandleException(ex, "Exception Policy");
				message = sb.ToString();
				return pi;
			}
			finally
			{
				if (reader != null)
				{
					if (!reader.IsClosed)
						reader.Close();
				}
				//if(conn != null)
				//    conn.Dispose();
			}
		}

		private static bool GetPatientNameFromNew(string patient, out string name, out string message)
		{
			StringBuilder sb = new StringBuilder();
			bool retVal = false;
			name = "";
			message = "";
			string sql = "SELECT LastName + ', ' + FirstName as Name " +
				" FROM PatientsNew WHERE MRN = '" + patient + "'";

			//SqlConnection conn = LoginForm.GetSqlConnection();
			IDataReader reader = null;
			try
			{
				reader = DataAccess.GetDataReader(sql);

				if (reader.Read())
				{
					name = reader.GetString(0);
					retVal = true;
				}
			}
			catch (Exception ex)
			{
				sb.Append("Could not retrieve patient name from new table!");
				sb.Append(Environment.NewLine);
				sb.Append("Error message: " + ex.Message);
				//bool b = //ExceptionPolicy.HandleException(ex, "Exception Policy");
				message = sb.ToString();
				return false;
			}
			finally
			{
				if (reader != null)
				{
					if (!reader.IsClosed)
						reader.Close();
				}
				//if(conn != null)
				//    conn.Dispose();
			}
			return retVal;
		}

        //public static Image SetLabelFormatID(string id, BarTender.Application btApp)
        //{
        //    btApp.ActiveFormat.SetNamedSubStringValue("ID", id);
        //    btApp.ActiveFormat.Save();
        //    return FormatToPicture(btApp);
        //}

        //private static Image FormatToPicture(BarTender.Application btApp)
        //{
        //    Image image = null;
        //    //Exporting format preview to clipboard			
        //    btApp.ActiveFormat.ExportToClipboard(BarTender.BtColors.btColors16, BarTender.BtResolution.btResolutionPrinter);

        //    //Defining a variable of type Bitmap to hold the exported image
        //    Bitmap bmpFromClipboard;

        //    //Defining a variable to store the Clipboard data
        //    IDataObject iData = Clipboard.GetDataObject();

        //    if (iData.GetDataPresent(typeof(System.Drawing.Bitmap)))
        //    {
        //        //If it is, then display it in an image
        //        bmpFromClipboard = (Bitmap)iData.GetData(typeof(System.Drawing.Bitmap));
        //        image = bmpFromClipboard;
        //    }

        //    return image;
        //}

	}
}
